package cn.yitun.chat.yitunservercommonlib.util;

import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.login.IpAddrVo;
import org.lionsoul.ip2region.DataBlock;
import org.lionsoul.ip2region.DbConfig;
import org.lionsoul.ip2region.DbSearcher;
import org.lionsoul.ip2region.Util;

import java.io.File;
import java.lang.reflect.Method;

/**
 * ip归属地等信息查询api
 */
public class IpUtil {


    /**
     * 获取城市定位
     * @param ip
     * @return
     */
    public static IpAddrVo getCityInfo(String ip){

        String ipAddr = getIpAddr(ip);
        if (YitunStringUtil.isNotEmpty(ipAddr)){
            IpAddrVo ipAddrVo = new IpAddrVo();
            String[] params = ipAddr.split("\\|");

            if (params.length > 0 ){
                ipAddrVo.setIp(ip);
                ipAddrVo.setCountry("0".equals(params[0])?"": params[0]);
                ipAddrVo.setCode(params[1]);
                ipAddrVo.setProvince("0".equals(params[2])?"": params[2]);
                ipAddrVo.setCity("0".equals(params[3])?"": params[3]);
                ipAddrVo.setServiceComp("0".equals(params[4])?"": params[4]);
                return ipAddrVo;
            }
        }

        return null;
    }

    /**
     * 采用ip2region本地解析获 归属地
     * 获取原始城市定位
     * @param ip
     * @return
     */
    public static String getIpAddr(String ip){

        //db
        String dbPath = IpUtil.class.getResource("/opt/ip2region.db").getPath();
//        String dbPath = "/data/util/ip2region.db";
//        String dbPath = "F:/ip2region.db";

        File file = new File(dbPath);
        try {
            if ( file.exists() == false ) {
                dbPath = "/data/util/ip2region.db";
            }
            //查询算法
            int algorithm = DbSearcher.BTREE_ALGORITHM; //B-tree
            //DbSearcher.BINARY_ALGORITHM //Binary
            //DbSearcher.MEMORY_ALGORITYM //Memory

            DbConfig config = new DbConfig();
            DbSearcher searcher = new DbSearcher(config, dbPath);

            //define the method
            Method method = null;
            switch ( algorithm )
            {
                case DbSearcher.BTREE_ALGORITHM:
                    method = searcher.getClass().getMethod("btreeSearch", String.class);
                    break;
                case DbSearcher.BINARY_ALGORITHM:
                    method = searcher.getClass().getMethod("binarySearch", String.class);
                    break;
                case DbSearcher.MEMORY_ALGORITYM:
                    method = searcher.getClass().getMethod("memorySearch", String.class);
                    break;
            }

            DataBlock dataBlock = null;
            if ( Util.isIpAddress(ip) == false ) {
                System.out.println("Error: Invalid ip address");
            }

            dataBlock  = (DataBlock) method.invoke(searcher, ip);

            return dataBlock.getRegion();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


}
